﻿using System;

namespace SBPweb.MediaCenter.Tools.WF
{

    public class ControlAnimationVector
    {

        private float pStartX;
        private float pStartY;
        private float pCurrentX;
        private float pCurrentY;
        private float pTargetX;
        private float pTargetY;
        private float pSpeed = DEFAULT_SPEED_FACTOR;

        public const float DEFAULT_SPEED_FACTOR = 0.1f;

        public ControlAnimationVector(int startX, int startY, int targetX, int targetY)
            :this(startX, startY, targetX, targetY, DEFAULT_SPEED_FACTOR)
        {
        }

        public ControlAnimationVector(int startX, int startY, int targetX, int targetY, float speed)
        {
            pStartX = pCurrentX = startX;
            pStartY = pCurrentY = startY;
            pTargetX = targetX;
            pTargetY = targetY;
        }

        public float StartX
        {
            get
            {
                return pStartX;
            }
        }

        public float StartY
        {
            get
            {
                return pStartY;
            }
        }

        public float TargetX
        {
            get
            {
                return pTargetX;
            }
            set
            {
                pTargetX = value;
            }
        }

        public float TargetY
        {
            get
            {
                return pTargetY;
            }
            set
            {
                pTargetY = value;
            }
        }

        public bool NeedsAnimation
        {
            get
            {
                return (pTargetX != pStartX) || (pTargetY != pStartY);
            }
        }

        public float GetNextX()
        {
            return pCurrentX = Lerp(pCurrentX, TargetX, pSpeed);
        }

        public float GetNextY()
        {
            return pCurrentY = Lerp(pCurrentY, TargetY, pSpeed);
        }

        public static float Lerp(float start, float end, float factor)
        {
            return start + factor * (end - start);
        }

    }

}